Before you start
with the exercise, here's a quick recap of
what you've learned so far. A Messaging System has
the following parts, a producer is a user application
that sends messages, A queue is a buffer
that stores messages, A consumer is a user application
that receives messages. Messages placed onto
the queue are stored, and available until the
consumer retrieves them. The core idea of the
Messaging Model in RabbitMQ, is that the producer can send messages only to an exchange, and the exchange pushes
them onto the queues. In order to do this, the exchange must know what to do with the message it receives, whether to append it
to a particular queue, or multiple queues, or to discard the message, which is defined by
the exchange type. Some of the exchange
types are direct, topic, header and fanout. However, it is possible to send messages to queues even
without an exchange. It is possible because of the default exchange
that RabbitMQ uses. In this example, the
sendToQueue method is directly called without
specifying the exchange type. In this case, the default or nameless exchange will
be used by RabbitMQ, and messages will be
routed to the queue with the name specified as
the first parameter. Let's now briefly talk
about the fanout exchange. The fanout exchange
is very simple, it just broadcasts
all the messages it receives to all
the queues it knows. Creating a fanout exchange
with name logs in a producer and consumer
can be done as shown. This code publishes the named exchange
logs in the producer. The relationship
between an exchange and a queue is called a binding, and it is done using the
bindQueue method as shown. The code to consume
messages from the queue using the consume method
is shown on the screen. Here's an introduction to
the challenge exercise. A logging system generates a detailed set of events that happen within
an application. Logging helps to troubleshoot your system when
an error occurs. It can help in tracing user
details in web applications. It delivers log messages to multiple consumers
or applications, that require data
from the user logs collected using the
publish-subscribe model. Now, using the knowledge
you've gained so far, perform an exercise to
create a logging system. These are the tasks
to be performed, pull the image of RabbitMQ and run a Docker
container for the same. Use the npm install command to install the amqplib
dependencies. Create a producer
that will produce log messages to
all its consumers. Broadcast the log
messages to consumers, and use the fanout exchange. Create a consumer to consume
the messages from the queue. Execute the producer program
using the node command. Create multiple instances
of the consumer program. In the RabbitMQ management, check if the messages get broadcasted to all
consumer instances. While performing the
challenge exercise, you must ensure to install
Docker on your system. Start the RabbitMQ
server on Docker by executing the docker run
command on the command prompt. Access the RabbitMQ
server console at http://localhost:15672
in the browser. Login to the server using guest, and guest as the username
and password respectively.